package reuseplugin.actions.eintrag;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import reuseplugin.Activator;
import reuseplugin.ClientInterface;

import common.administration.Benutzer;
import common.itemhandling.Eintrag;

/**
 * @author Julius Ollesch
 * 
 */
public class EintragMeldenAction extends Action implements ClientInterface,
		IWorkbenchWindowActionDelegate {
	private TableViewer viewer;

	public EintragMeldenAction(TableViewer v) {
		super();
		viewer = v;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IWorkbenchWindow window) {
		// TODO Auto-generated method stub

	}

	private void make() {

		ISelection selection = viewer.getSelection();
		Eintrag eintrag = (Eintrag) ((IStructuredSelection) selection)
				.getFirstElement();
		if (eintrag != null) {
			/**
			 * Abfrage ob Session aktiv
			 */
			if (Activator.getDefault().c.isAktiv()) {
				Benutzer admin = Activator.getDefault().c
						.benutzerAnzeigen(Activator.getDefault().c
								.getBenutzer().getErsteller());
				if (admin != null) {
					String adminEMail = admin.getEmailAdresse();

					String benutzerEMail = Activator.getDefault().c
							.getBenutzer().getEmailAdresse();
					String betreff = (Messages.EintragMeldenAction_mailSubject + eintrag
							.getName());

					String mailto = adminEMail + "?cc=" //$NON-NLS-1$
							+ benutzerEMail + "?subject=" + betreff; //$NON-NLS-1$

					if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
						try {
							URI mailtoUri = new URI("mailto", mailto, null); //$NON-NLS-1$

							Desktop.getDesktop().browse(mailtoUri);
						} catch (IOException e) {
							MessageDialog
									.openError(
											Display.getCurrent()
													.getActiveShell(),
											Messages.EintragMeldenAction_errorTitle,
											Messages.EintragMeldenAction_cannotSendEmail);
						} catch (URISyntaxException e) {
							MessageDialog
									.openError(
											Display.getCurrent()
													.getActiveShell(),
											Messages.EintragMeldenAction_errorTitle,
											Messages.EintragMeldenAction_cannotSendEmail);
						}
					}

				} else {
					MessageDialog.openError(Display.getCurrent()
							.getActiveShell(),
							Messages.EintragMeldenAction_errorTitle,
							Messages.EintragMeldenAction_currentIsAdmin);
				}
			}
		} else {
			MessageDialog.openError(Display.getCurrent().getActiveShell(),
					Messages.EintragMeldenAction_errorTitle,
					Messages.EintragMeldenAction_selectItem);
		}

	}

	@Override
	public void run() {
		make();
	}

	@Override
	public void run(IAction action) {
		make();
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub

	}
}
